<!DOCTYPE html>
<html>
<head>
<include file="Include:meta" />
<title>添加运营周报-目的地运营集团数据管理平台</title>
<include file="Include:style" />
</head>
<body>
<include file="Include:header" />
<include file="Include:left" />
<div id="main">
	<div class="crumbs">
		<div class="crumbs-text">项目 > [{$project['project_num']}]{$project['project_name']} > 运营周报 > 添加运营周报</div>
	</div>
	<div id="content">
		<form action="__APP__/Project/project_operate_add" method="post" enctype="multipart/form-data">
			<input type="hidden" name="action" value="add">
			<input type="hidden" name="pid" value="{$project['id']}">
			<div class="form-main">
				<div class="form-title">添加运营周报</div>
				<div class="form-item">
					<div class="input-label"><span class="asterisk">*</span>周期</div>
					<div class="input-item">
						<select name="year" class="form-control w100" autocomplete="off">
							<foreach name="year_list" item="vo">
								<option value="{$vo}" <if condition="$vo eq $year">selected</if>>{$vo}</option>
							</foreach>
						</select>
						年
						<select name="week" class="form-control w250 ml10 required-select" autocomplete="off">
							<option value="-10" selected>请选择</option>
							<foreach name="week_list" item="vo">
								<option value="{$vo['num']}">{$vo['detail']}</option>
							</foreach>
						</select>
						周
						<span class="error-item">必填项</span>
					</div>
				</div>
				<div class="form-item">
					<div class="input-label">附件</div>
					<div class="input-item">
						<div class="input-group w250 left">
							<input type="text" name="file_name" class="form-control file-name" placeholder="请上传附件" readonly autocomplete="off">
							<span class="input-group-btn">
								<button class="btn file-submit" type="button">上传</button>
							</span>
						</div>
						<button class="btn file-delete ml5 hide" type="button">删除</button>
						<input type="hidden" name="file_url" class="file-url" autocomplete="off">
						<span class="error-item">文件名必填</span>
					</div>
				</div>
				<div class="form-sub-title">工作汇报</div>
				<div class="form-table-item">
					<table class="table">
						<tr class="form-table-header">
							<td width="15%"><span class="asterisk">*</span>一级类</td>
							<td width="15%"><span class="asterisk">*</span>二级类</td>
							<td width="70%"><span class="asterisk">*</span>内容</td>
						</tr>
						<foreach name="gzhb" item="vo">
							<foreach name="vo['second']" item="wo">
								<if condition="$wo['third'] eq NULL">
									<tr class="hb_tr2">
										<td>{$wo['second_name']}</td>
										<td></td>
										<td>
											<input type="hidden" name="hb_sid_copy[]" class="hb_sid" value="{$wo['id']}">
											<input type="hidden" name="hb_tid_copy[]" class="hb_tid" value="-10">
											<textarea name="hb_content_copy[]" class="form-control w600 required-text" autocomplete="off"></textarea>
											<span class="error-item">必填项</span>
										</td>
									</tr>
								<else />
									<foreach name="wo['third']" item="vv" key="vvkey">
									<tr class="hb_tr2">
										<if condition="$vvkey eq 0"><td  rowspan="<?php echo count($wo['third']);?>" >{$wo['second_name']}</td></if>
										<td>{$vv['third_name']}</td>
										<td>
											<input type="hidden" name="hb_sid_copy[]" class="hb_sid" value="{$wo['id']}">
											<input type="hidden" name="hb_tid_copy[]" class="hb_tid" value="{$vv['id']}">
											<textarea name="hb_content_copy[]" class="form-control w600 required-text" autocomplete="off"></textarea>
											<span class="error-item">必填项</span>
										</td>
									</tr>
									</foreach>
								</if>
							</foreach>
						</foreach>
					</table>
				</div>
			</div>
			<div class="form-btn-item"><button class="btn submit" type="submit">提交</button><button class="btn btn-default goback" type="button">返回</button></div>
		</form>
	</div>
</div>
<!--添加景区名称对话框-->
<div class="modal fade" id="nameModal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4>添加景区名称</h4>
				<i class="fa fa-times" aria-hidden="true" data-dismiss="modal"></i>
			</div>
			<div class="modal-body">
				<strong class="mr5"><span class="asterisk">*</span>景区名称</strong>
				<input type="text" class="modal-add-name form-control w300" autocomplete="off">
			</div>
			<div class="modal-footer">
				<button type="button" class="btn name-submit">确认</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div>
	</div>
</div>
<!--重置景区名称对话框-->
<div class="modal fade" id="resetModal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h4>重置景区名称</h4>
				<i class="fa fa-times" aria-hidden="true" data-dismiss="modal"></i>
			</div>
			<div class="modal-body">您确定要重置景区名称吗？一但重置周期和所有日期将会清空！</div>
			<div class="modal-footer">
				<button type="button" class="btn confirm">确认</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div>
	</div>
</div>
<include file="Include:javascript" />
<script type="text/javascript">
$(function(){
	KindEditor.ready(function(K){
		//上传附件
		var editorFile = K.editor();
		$('.file-submit').on('click',function(){
			var parents = $(this).parents(".input-group");
			editorFile.loadPlugin('insertfile',function(){
				document.cookie="file_name=weekly";
				document.cookie="file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
				editorFile.plugin.fileDialog({
					clickFn:function(url,title){
						parents.nextAll(".error-item").hide();
						parents.find(".file-name").val(title).prop("readonly",false);
						parents.nextAll(".file-url").val(url);
						parents.nextAll(".file-delete").show();
						editorFile.hideDialog();
					}
				});
			});
		});
	});
	
	//删除附件
	$(".file-delete").click(function(){
		$(this).hide();
		$(this).nextAll(".error-item").hide();
		$(this).nextAll(".file-url").val("");
		$(this).prevAll(".input-group").find(".file-name").val("").prop("readonly",true);
		$(this).parents("tr").find(".required-text2").next(".error-item").hide();
	});
	//工作汇报联动
	$('body').on("change","[name='hb_sid[]']",function(){
		var _this = $(this),
			sid = _this.val();
		$.ajax({
			type:"POST",
			asnyc:true,
			dataType:"json",
			data:{"sid":sid,"action":"get_gzhb"},
			url:"__APP__/Project/project_operate_ajax",
			success:function(data){
				if(data.status==1){
					var option = "<option value='-10' selected>请选择</option>";
					for(var x in data.date){
						option += "<option value="+data.date[x].id+">"+data.date[x].third_name+"</option>";
					}
					_this.parents('tr').find("[name='hb_tid[]']").html(option).trigger("change").parent('div').removeClass('hide');
				}else{
					_this.parents('tr').find("[name='hb_tid[]']").html("<option value='-10' selected>请选择</option>").trigger("change").parent('div').addClass('hide');
				}
			}
		});
	})
	//经营数据联动
	$('body').on("change","[name='sj_sid[]']",function(){
		var _this = $(this),
			sid = _this.val();
		$("[name='sj_pid[]']").val(-10);
		$(".input").val('');
		$.ajax({
			type:"POST",
			asnyc:true,
			dataType:"json",
			data:{"sid":sid,"action":"get_jysj"},
			url:"__APP__/Project/project_operate_ajax",
			success:function(data){
				if(data.status==1){
					var option = "<option value='-10' selected>请选择</option>";
					for(var x in data.date){
						option += "<option value="+data.date[x].id+">"+data.date[x].third_name+"</option>";
					}
					_this.parents('tr').find("[name='sj_tid[]']").html(option).trigger("change");
				}else{
					_this.parents('tr').find("[name='sj_tid[]']").html("<option value='-10' selected>请选择</option>").trigger("change");
				}
			}
		});
		if(sid==254){
			_this.parents('tr').find("[name='sj_pid[]']").parent('div').removeClass('hide');
			_this.parents('tr').find("[name='number[]']").removeClass('hide').nextAll('.error-item').hide();
			_this.parents('tr').find('select').nextAll('.error-item').hide();
		}else{
			_this.parents('tr').find("[name='sj_pid[]']").parent('div').addClass('hide');
			_this.parents('tr').find("[name='number[]']").addClass('hide').nextAll('.error-item').hide();
			_this.parents('tr').find('select').nextAll('.error-item').hide();
		}
	});
	//年联动周
	$("[name='year']").on("change",function(){
		ajaxWeek();
	});
	
	//ajax周
	function ajaxWeek(){
		var year = $("[name='year']").val(),
			pid = "{$project['id']}";
		$.ajax({
			type:"POST",
			asnyc:true,
			dataType:"json",
			data:{"pid":pid,"year":year,"action":"get_week"},
			url:"__APP__/Project/project_operate_ajax",
			success:function(data){
				var option = "<option value='-10' selected>请选择</option>";
				for(var x in data){
					option += "<option value="+data[x].num+">"+data[x].detail+"</option>";
				}
				$("[name='week']").html(option).trigger("change");
			}
		});
	}
	//添加工作汇报
	$(".data-add").on("click",function(){
		var state = true;
		$("[name='hb_sid[]']").each(function(){
			var val = $(this).val();
			if(val==-10){
				$(this).nextAll(".error-item").eq(0).show();
				state = false;
			}
		});
		$("[name='hb_tid[]']").each(function(){
			_this = $(this);
			if($(this).parent('div').hasClass('hide')){
			
			}else{
				var val = _this.val();
				if(val==-10){
					$(this).nextAll(".error-item").eq(0).show();
					state = false;
				}
			}
		})
		$("[name='hb_content[]']").each(function(){
			var val = $(this).val();
			if(val=="" || /^\s+$/.test(val)){
				$(this).next(".error-item").show();
				state = false;
			}
		});
		//重复分类判断
		var hb_l = $('.hb_tr').length,
			val_hb_tr = [];
		$('.hb_tr2').each(function(i){
			var val1 = $('.hb_tr2').eq(i).find(".hb_sid").val(),
				val2 = $('.hb_tr2').eq(i).find(".hb_tid").val();
			val_hb_tr[i] = val1+val2;
		});
		function arrRepeat(arr){
			var arrStr = JSON.stringify(arr),str;
			for(var i=0;i<arr.length;i++){
				if(arrStr.indexOf(arr[i])!=arrStr.lastIndexOf(arr[i])){
					return true;
				}
			}
			return false;
		}
		if(arrRepeat(val_hb_tr)){
			defeated_alert("工作汇报分类不能重复");
			state = false;
		}
		
		if(state){
			var tr = $(this).parents("tr"),
				table = $(this).parents("table"),
				sid = $("[name='hb_sid[]']").val(),
				sid_op = $("[name='hb_sid[]']").find('option:selected').text(),
				tid = $("[name='hb_tid[]']").val(),
				tid_op = $("[name='hb_tid[]']").find('option:selected').text(),
				content = $("[name='hb_content[]']").val(),
				html = "";
			html+= "<tr class='hb_tr hb_tr2'><td><input type='hidden' name='hb_sid_copy[]' class='hb_sid' value='"+sid+"'>"+sid_op+"</td><td><input type='hidden' name='hb_tid_copy[]' class='hb_tid' value='"+tid+"'>";
			if($("[name='hb_tid[]']").parent('div').hasClass('hide')){}else{
				html+= ""+tid_op+"";
			};
			html+= "</td><td><input type='hidden' name='hb_content_copy[]' value='"+content+"'>"+content+"</td><td><a href='javascript:;' class='data-delete' data-toggle='tooltip' data-placement='bottom' title='删除'><i class='glyphicon glyphicon-trash'></i></a></td></tr>";
			table.find(".error-item").hide();
			table.find(".select2,.tooltip").remove();
			table.find("select").val(-10);
			table.find("textarea").val('');
			$("[name='hb_tid[]']").parent('div').addClass('hide');
			setTimeout(function(){
				table.find(".data-delete").tooltip({trigger:'hover'});
			},200);
			table.append(html);
			table.find("tr select").select2();
		}
	});
	//删除经营数据
	$(document).on("click",".data-delete",function(){
		$(this).parents("tr").remove();
	});
	
	//添加经营数据
	$(".mission-add").on("click",function(){
		var state = true;
		$("[name='number[]']").each(function(){
			var val = $(this).val();
			if($(this).hasClass('hide')){
				
			}else{
				if(val==""){
					$(this).nextAll(".error-item").eq(0).show();
					state = false;
				}else if(!/^[1-9]*[1-9][0-9]*$/.test(val) || val==0){
					$(this).nextAll(".error-item").eq(1).show();
					state = false;
				}
			}
		});
		$("[name='sj_sid[]']").each(function(){
			var val = $(this).val();
			if(val==-10){
				$(this).nextAll(".error-item").eq(0).show();
				state = false;
			}
		});
		$("[name='sj_tid[]']").each(function(){
			var val = $(this).val();
			if(val==-10){
				$(this).nextAll(".error-item").eq(0).show();
				state = false;
			}
		});
		$("[name='sj_pid[]']").each(function(){
			_this = $(this);
			if($(this).parent('div').hasClass('hide')){
			}else{
				var val = _this.val();
				if(val==-10){
					$(this).nextAll(".error-item").eq(0).show();
					state = false;
				}
			}
		})
		$("[name='money[]']").each(function(){
			var val = $(this).val();
			if(val==""){
				$(this).nextAll(".error-item").eq(0).show();
				state = false;
			}else if(!/(^0\.\d{1,2}$)|(^[1-9][0-9]*(\.\d{1,2})?$)/.test(val) || val==0){
				$(this).nextAll(".error-item").eq(1).show();
				state = false;
			}
		});
		//重复分类判断
		var val_sj_tr = [];
		$('.sj_tr2').each(function(i){
			var val1 = $('.sj_tr2').eq(i).find(".sj_sid").val(),
				val2 = $('.sj_tr2').eq(i).find(".sj_tid").val(),
				val3 = $('.sj_tr2').eq(i).find(".sj_pid").val();
			val_sj_tr[i] = val1+val2+val3;
		});
		function arrRepeat(arr){
			var arrStr = JSON.stringify(arr),str;
			for(var i=0;i<arr.length;i++){
				if(arrStr.indexOf(arr[i])!=arrStr.lastIndexOf(arr[i])){
					return true;
				}
			}
			return false;
		}
		if(arrRepeat(val_sj_tr)){
			defeated_alert("经营数据分类不能重复");
			state = false;
		}
		
		if(state){
			var tr = $(this).parents("tr"),
				table = $(this).parents("table"),
				sid = $("[name='sj_sid[]']").val(),
				sid_op = $("[name='sj_sid[]']").find('option:selected').text(),
				tid = $("[name='sj_tid[]']").val(),
				tid_op = $("[name='sj_tid[]']").find('option:selected').text(),
				pid = $("[name='sj_pid[]']").val(),
				pid_op = $("[name='sj_pid[]']").find('option:selected').text(),
				num = $("[name='number[]']").val(),
				money = $("[name='money[]']").val(),
				content = $("[name='sj_content[]']").val(),
				html = "";
			html+= "<tr class='sj_tr sj_tr2'><td><input type='hidden' name='sj_sid_copy[]' class='sj_sid' value='"+sid+"'>"+sid_op+"</td><td><input type='hidden' name='sj_tid_copy[]' class='sj_tid' value='"+tid+"'>"+tid_op+"</td><td><input type='hidden' name='sj_pid_copy[]' class='sj_pid' value='"+pid+"'>";
			if($("[name='sj_pid[]']").parent('div').hasClass('hide')){}else{
				html+= ""+pid_op+"";
			};
			html+= "</td><td><input type='hidden' name='number_copy[]' value='"+num+"'>"+num+"</td><td><input type='hidden' name='money_copy[]' value='"+money+"'>"+money+"</td><td><a href='javascript:;' class='data-delete' data-toggle='tooltip' data-placement='bottom' title='删除'><i class='glyphicon glyphicon-trash'></i></a></td></tr>";
			table.find(".error-item").hide();
			table.find(".select2,.tooltip").remove();
			table.find("select").val(-10);
			table.find(".input").val('');
			$("[name='sj_pid[]']").parent('div').addClass('hide');
			$("[name='number[]']").addClass('hide');
			setTimeout(function(){
				table.find(".data-delete").tooltip({trigger:'hover'});
			},200);
			table.append(html);
			table.find("tr select").select2();
		}
	});
	
	//删除计划任务
	$(document).on("click",".mission-delete",function(){
		$(this).parents("tr").remove();
	});

	//提交验证
	$(".submit").on('click',function(){
		var tag = false;
		
		$('.required-text').each(function(){
			var val = $(this).val();
			if(val=="" || /^\s+$/.test(val)){
				$(this).next(".error-item").show();
				tag = true;
			}
		})
		$(".required-select").each(function(){
			var val = $(this).val();
			if(val==-10){
				$(this).nextAll(".error-item").eq(0).show();
				tag = true;
			}
		});
		$(".file-name").each(function(){
			var val = $(this).val();
			if((val=="" || /^\s+$/.test(val)) && !$(this).prop("readonly")){
				$(this).parents(".input-group").nextAll(".error-item").show();
				tag = true;
			}
		});
		$(".file-url").each(function(){
			var val = $(this).val();
			if(val!=""){
				$(this).parents("tr").find(".required-text2").each(function(){
					var val2 = $(this).val();
					if(val2=="" || /^\s+$/.test(val2)){
						$(this).next(".error-item").show();
						tag = true;
					}
				});
			}
		});
		$(".required-text2").each(function(){
			var val = $(this).val();
			if(val!=""){
				$(this).parents("tr").find(".required-text2").each(function(){
					var val2 = $(this).val();
					if(val2=="" || /^\s+$/.test(val2)){
						$(this).next(".error-item").show();
						tag = true;
					}
				});
			}
		});
		if(tag){
			return false;
		}
	});
	
	//隐藏错误提示
	$(document).on("focus",".required-text,[name='money[]'],[name='number[]'],[name='hb_content[]']",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(document).on("change","[name='hb_sid[]'],.required-select",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(document).on("change","[name='hb_tid[]']",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(document).on("change","[name='sj_pid[]']",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(document).on("change","[name='sj_sid[]']",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(document).on("change","[name='sj_tid[]']",function(){
		$(this).nextAll(".error-item").hide();
	});
	$(".file-name").on("focus",function(){
		$(this).parents(".input-group").nextAll(".error-item").hide();
	});
	$(".required-text2").on("focus",function(){
		$(this).parents("tr").find(".required-text2").nextAll(".error-item").hide();
	});
});
</script>
</body>
</html>